package com.spring.comment.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.spera.genutil.TokenUtil;
import com.spring.comment.web.dao.CommentMapper;
import com.spring.comment.web.service.CommentService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.tools.MyTools;

@Service
public class CommentServiceImpl implements CommentService{
	@Resource
	private CommentMapper commentMapper;
	
	@Resource
	private JedisCache jedisCache;
	

	
	//评论服务 
	@Override
	public Map<String, Object> insertCommentService(String userID,String userType,String userName,String serviceID,String businessTypeCode,
			String serviceModuleCode,String comment) throws Exception {
			Map<String, Object> m = new HashMap<String, Object>();
			
			HashMap hmc=MyTools.checkIsNotNull(userID,userType,userName,serviceID,businessTypeCode,serviceModuleCode,comment);
			if(hmc!=null){
				return hmc;
			}
			
//			HashMap<String,Object> hm=new HashMap<String,Object>();
//			hm.put("userID", userID);
//			hm.put("userType", userType);
//			hm.put("userName", userName);
//			hm.put("serviceID", serviceID);
//			hm.put("businessTypeCode", businessTypeCode);
//			hm.put("serviceModuleCode", serviceModuleCode);
//			hm.put("comment", comment);
//			
//			
//			int effect = commentMapper.insertCommentService(hm);
//			
			
			String uuid=ApplicationUtils.randomUUID();
			HashMap<String,Object> hm=new HashMap<String,Object>();
			hm.put("uuid", uuid);
			hm.put("toUserID", null);
			hm.put("toUserType", null);
			hm.put("toUserName", null);
			hm.put("fromUserID", userID);
			hm.put("fromUserType", userType);
			hm.put("fromUserName", userName);
			hm.put("serviceID", serviceID);
			hm.put("businessTypeCode", businessTypeCode);
			hm.put("serviceModuleCode", serviceModuleCode);
			hm.put("comment", comment);
						
			int effect = commentMapper.insertCommentCustomerService(hm);
			
			
			if(effect==0){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "评论失败");
			}else{
				m.put(ApplicationUtils.STATUS, "1");
				m.put(ApplicationUtils.MSG, "评论成功");
			}

	
		return m;
	}
	
	//插入服务评论
	@Override
	public Map<String, Object> createCommentService(String token, String serviceID, String businessTypeCode, 
			String serviceModuleCode, String comment) throws Exception {
			Map<String, Object> m = new HashMap<String, Object>();
			
			
			HashMap hmc=MyTools.checkIsNotNull(serviceID,businessTypeCode,serviceModuleCode,comment);
			
			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}

			String userID = jedisCache.hget(token, TokenUtil.USER_ID);
			String userName = jedisCache.hget(token, TokenUtil.USER_NAME);//待该
			String userType="1";
			//String userType = jedisCache.lindex(token, 4);//待该
			
			
			if(hmc!=null){
				return hmc;
			}
			
			m= this.insertCommentService(userID, userType, userName, serviceID, businessTypeCode, serviceModuleCode, comment);

		return m;
	}
	
	
	
	

	//评论服务 
		@Override
		public Map<String, Object> insertCommentCustomerService(String toUserID,String toUserType,String toUserName,
				String fromUserID,String fromUserType,String fromUserName,String serviceID,String businessTypeCode,
				String serviceModuleCode,String comment) throws Exception {
				Map<String, Object> m = new HashMap<String, Object>();
				
				HashMap<String,Object> hmc=MyTools.checkIsNotNull(fromUserID,fromUserType,fromUserName,serviceID,businessTypeCode,serviceModuleCode,comment);
				if(hmc!=null){
					return hmc;
				}
				
				String uuid=ApplicationUtils.randomUUID();
				HashMap<String,Object> hm=new HashMap<String,Object>();
				hm.put("uuid", uuid);
				hm.put("toUserID", toUserID);
				hm.put("toUserType", toUserType);
				hm.put("toUserName", toUserName);
				hm.put("fromUserID", fromUserID);
				hm.put("fromUserType", fromUserType);
				hm.put("fromUserName", fromUserName);
				hm.put("serviceID", serviceID);
				hm.put("businessTypeCode", businessTypeCode);
				hm.put("serviceModuleCode", serviceModuleCode);
				hm.put("comment", comment);
				
				
				int effect = commentMapper.insertCommentCustomerService(hm);
				
				if(effect==0){
					m.put(ApplicationUtils.STATUS, "0");
					m.put(ApplicationUtils.MSG, "评论失败");
				}else{
					m.put(ApplicationUtils.DATASET, uuid);
					m.put(ApplicationUtils.STATUS, "1");
					m.put(ApplicationUtils.MSG, "评论成功");
				}

		
			return m;
		}
		

		
		@Override
		public Map<String, Object> createCommentCustomerService(String token, String serviceID,String toUserID,String toUserType,
				String toUserName, String businessTypeCode, String serviceModuleCode, String comment) throws Exception {
				Map<String, Object> m = new HashMap<String, Object>();
				
				Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
				if (t != null) {
					return t;
				}
				
				HashMap hmc=MyTools.checkIsNotNull(serviceID,businessTypeCode,serviceModuleCode,comment);
				if(hmc!=null){
					return hmc;
				}
				String fromUserID = jedisCache.hget(token, TokenUtil.USER_ID);
				String fromUserName = jedisCache.hget(token, TokenUtil.USER_NAME);//待该
				String fromUserType="1";
				
				m=this.insertCommentCustomerService(toUserID, toUserType, toUserName, fromUserID, fromUserType, fromUserName, serviceID, businessTypeCode, serviceModuleCode, comment);
				return m;

		}
				
		
		
	//得到评论服务
//	@Override
	public Map<String, Object> getCommentService(String businessTypeCode,String serviceID,String userID,
			String serviceModuleCode,String pageSize,String pageIndex,String skip,String rows) throws Exception {
		Map<String, Object> m = new HashMap<String, Object>();
	
		
		HashMap<String,Object> hmc=MyTools.checkIsNotNull(businessTypeCode,serviceID,serviceModuleCode);
		if(hmc!=null){
			return hmc;
		}
		
//		HashMap<String,Object> hm=MyTools.pageIndexToSkipSearchAllLimit(pageSize, pageIndex, skip, rows);
//		hm.put("businessTypeCode", businessTypeCode);
//		hm.put("serviceID", serviceID);
//		hm.put("userID", userID);
//		hm.put("serviceModuleCode", serviceModuleCode);
//		
//		
//		List<HashMap<String, Object>> list = commentMapper.getCommentService(hm);
		
		HashMap<String,Object> hm=MyTools.pageIndexToSkipSearchAllLimit(pageSize, pageIndex, skip, rows);
		hm.put("businessTypeCode", businessTypeCode);
		hm.put("serviceID", serviceID);
		hm.put("toUserID", null);
		hm.put("fromUserID", userID);
		hm.put("serviceModuleCode", serviceModuleCode);
				
		List<HashMap<String, Object>> list = commentMapper.getCommentCustomer(hm);
		
		
		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}
				
				

		//得到评论服务
//		@Override
//		public Map<String, Object> getCommentServiceItem(String businessTypeCode,String serviceID,String userID,
//				String serviceModuleCode,String pageSize,String pageIndex,String skip,String rows) throws Exception {
//			Map<String, Object> m = new HashMap<String, Object>();
//			
//			HashMap<String,Object> hmc=MyTools.checkIsNotNull(businessTypeCode,serviceID,serviceModuleCode);
//			if(hmc!=null){
//				return hmc;
//			}
//			
//			HashMap<String,Object> hm=MyTools.pageIndexToSkipSearchAllLimit(pageSize, pageIndex, skip, rows);
//			hm.put("businessTypeCode", businessTypeCode);
//			hm.put("serviceID", serviceID);
//			hm.put("userID", userID);
//			hm.put("serviceModuleCode", serviceModuleCode);
//			
//			
//			List<com.spring.comment.web.model.CommentService> list = commentMapper.getCommentServiceItem(hm);
//			
//	
//			
//			m.put(ApplicationUtils.DATASET, list);
//			m.put(ApplicationUtils.STATUS, "1");
//			m.put(ApplicationUtils.MSG, "查询成功");
//	
//			return m;
//		}
		
		//
		@Override
		public Map<String, Object> getCommentItem(String businessTypeCode,String serviceID,String fromUserID,String toUserID,
				String serviceModuleCode,String pageSize,String pageIndex,String skip,String rows) {
			Map<String, Object> m = new HashMap<String, Object>();
			
			HashMap<String,Object> hmc=MyTools.checkIsNotNull(businessTypeCode,serviceID,serviceModuleCode);
			if(hmc!=null){
				return hmc;
			}
			
			HashMap<String,Object> hm=MyTools.pageIndexToSkipSearchAllLimit(pageSize, pageIndex, skip, rows);
			hm.put("businessTypeCode", businessTypeCode);
			hm.put("serviceID", serviceID);
			hm.put("fromUserID", fromUserID);
			hm.put("toUserID", toUserID);
			hm.put("serviceModuleCode", serviceModuleCode);
						
			List<com.spring.comment.web.model.CommentService> list = commentMapper.getCommentItem(hm);
		
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");
	
			return m;
		}
		
		
		
		//得到评论人
		@Override
		public Map<String, Object> getCommentCustomer(String businessTypeCode,String serviceID,String toUserID,String fromUserID,
				String serviceModuleCode,String pageSize,String pageIndex,String skip,String rows) throws Exception {
			Map<String, Object> m = new HashMap<String, Object>();
		
			
			HashMap<String,Object> hmc=MyTools.checkIsNotNull(businessTypeCode,serviceID,serviceModuleCode);
			if(hmc!=null){
				return hmc;
			}
			
			HashMap<String,Object> hm=MyTools.pageIndexToSkipSearchAllLimit(pageSize, pageIndex, skip, rows);
			hm.put("businessTypeCode", businessTypeCode);
			hm.put("serviceID", serviceID);
			hm.put("toUserID", toUserID);
			hm.put("fromUserID", fromUserID);
			hm.put("serviceModuleCode", serviceModuleCode);
			
			
			List<HashMap<String, Object>> list = commentMapper.getCommentCustomer(hm);
	
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");
	
			return m;
		}



	

}
